﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MODULE_TIENICH
{
    public partial class frm_timkiemhanghoa : Form
    {
        int currenrowindex;
        tienichDataContext context = cls_tienich.getcontext();
        public string _mahanghoa
        
        {
            get { return txtmahanghoa.Text; }
            set { txtmahanghoa.Text = value; }

        }
        public frm_timkiemhanghoa()
        {
            InitializeComponent();
        }

        private void frm_timkiemhanghoa_Load(object sender, EventArgs e)
        {
          

            List<DM_hanghoa> hanghoas = context.DM_hanghoas.ToList();
            string machuhang = this.txtmahanghoa.Text;

            if (hanghoas.Count > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < hanghoas.Count(); i++)
                {
                    DM_hanghoa hanghoa = hanghoas.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["mahanghoa"].Value = hanghoa.mahang;
                    grdata.Rows[i].Cells["tenhanghoa"].Value = hanghoa.tenhang;
                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();

            }

        }

        private void grdata_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
            {

                this.Close();

            }
            if (e.KeyCode == Keys.Down)
            {
                try
                {
                    if (currenrowindex < grdata.Rows.Count-1)
                    {
                        currenrowindex =currenrowindex+1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                try
                {
                    if (currenrowindex > 0)
                    {
                        currenrowindex = currenrowindex-1;
                        this.grdata.Rows[currenrowindex].Selected = true;
                        this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void grdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                currenrowindex = e.RowIndex;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();
            }
            catch (Exception ex)
            {
            }
        }

        private void grdata_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    currenrowindex = e.RowIndex;
            //    this.grdata.Rows[currenrowindex].Selected = true;
            //    this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();
            //}
            //catch (Exception ex)
            //{
            //}
        }

        private void txtkeyword_TextChanged(object sender, EventArgs e)
        {
            string keyword = txtkeywordtenhang.Text;
            IEnumerable<DM_hanghoa> hanghoas = from c in context.DM_hanghoas where c.tenhang.Contains(keyword) select c;//context.DM_hanghoas.ToList();
           

            if (hanghoas.Count() > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < hanghoas.Count(); i++)
                {
                    DM_hanghoa hanghoa = hanghoas.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["mahanghoa"].Value = hanghoa.mahang;
                    grdata.Rows[i].Cells["tenhanghoa"].Value = hanghoa.tenhang;
                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();

            }

        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtkeywordmahang_TextChanged(object sender, EventArgs e)
        {
            string keyword = txtkeywordtenhang.Text;
            IEnumerable<DM_hanghoa> hanghoas = from c in context.DM_hanghoas where c.mahang.Contains(keyword) select c;//context.DM_hanghoas.ToList();


            if (hanghoas.Count() > 0)
            {
                grdata.Rows.Clear();
                for (int i = 0; i < hanghoas.Count(); i++)
                {
                    DM_hanghoa hanghoa = hanghoas.ElementAt(i);
                    grdata.Rows.Add();
                    grdata.Rows[i].Cells["mahanghoa"].Value = hanghoa.mahang;
                    grdata.Rows[i].Cells["tenhanghoa"].Value = hanghoa.tenhang;
                }
                currenrowindex = 0;
                this.grdata.Rows[currenrowindex].Selected = true;
                this.txtmahanghoa.Text = grdata.Rows[currenrowindex].Cells["mahanghoa"].Value.ToString();

            }


        }
    }
}
